#include "tcpsever.h"

TCPSever::TCPSever()
{
    InitServer();
}

void TCPSever::InitServer()
{
    mTcpServer = new QTcpServer(this);//初始化监听套接字
    mTcpSocket = new QTcpSocket;//初始化连接套接字
    mHostAddress.setAddress("10.0.0.177");//设置监听网卡IP
    quint16 _port = 7777;//设置监听端口
    mTcpServer->listen(mHostAddress,_port);//监听指定网卡和端口
    qDebug()<<"Listen Interface ["<<mHostAddress.toString()<<"] And Port ["<<_port<<"] Successful!"<<endl;
    connect(mTcpServer,&QTcpServer::newConnection,this,[=]{
        mTcpSocket = mTcpServer->nextPendingConnection();//获取连接套接字
        qDebug()<<"Connect To Client Successful!"<<endl;

        connect(mTcpSocket,&QTcpSocket::readyRead,this,[=]{//读取消息
            QByteArray _data = mTcpSocket->readAll();
            qDebug()<<"TCP Receive Data : "<<QString::fromLatin1(_data)<<endl;
        });
    });
 }
